package com.course.server.service;/**
 * @author: lin
 */

import com.course.server.domain.Chapter;
import com.course.server.domain.ChapterExample;
import com.course.server.dto.ChapterDto;
import com.course.server.dto.ChapterPageDto;
import com.course.server.mapper.ChapterMapper;
import com.course.server.util.CopyUtil;
import com.course.server.util.UuidUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 *@Author lin
 *@Data Created in 2020/7/28 21:21
 *Description 
 *@Modified By
 *@Version V1.0
 */
@Service
public class ChapterService {
    @Resource
    private ChapterMapper chapterMapper;
    /**
     * create by: lin
     * description: 列表查询
     * create time: 2020/8/2 17:40
     * @param chapterPageDto
     */
    public void list(ChapterPageDto chapterPageDto){
        PageHelper.startPage(chapterPageDto.getPage(),chapterPageDto.getSize());
        ChapterExample chapterExample = new ChapterExample();
        ChapterExample.Criteria criteria = chapterExample.createCriteria();
        if(!StringUtils.isEmpty(chapterPageDto.getCourseId())){
            criteria.andCourseIdEqualTo(chapterPageDto.getCourseId());
        }
        List<Chapter> chapterList = chapterMapper.selectByExample(chapterExample);
        PageInfo<Chapter> pageInfo = new PageInfo<>(chapterList);
        chapterPageDto.setTotal(pageInfo.getTotal());
        List<ChapterDto> chapterDtoList = CopyUtil.copyList(chapterList, ChapterDto.class);
        chapterPageDto.setList(chapterDtoList);
    }
    /**
     * create by: lin
     * description: 保存
     * create time: 2020/8/2 17:40
     */
    public void save(ChapterDto chapterDto){
        Chapter chapter = CopyUtil.copy(chapterDto, Chapter.class);
        if(StringUtils.isEmpty(chapterDto.getId())){
            //StringUtils.isEmpty spring的工具类来判断是否有值
            this.insert(chapter);
        }else {
            this.update(chapter);
        }
    }
    /**
     * create by: lin
     * description: 新增
     * create time: 2020/8/2 17:41
     */
    private void insert(Chapter chapter){
        chapter.setId(UuidUtil.getShortUuid());
        chapterMapper.insert(chapter);
    }
    /**
     * create by: lin
     * description: 修改
     * create time: 2020/8/2 17:58
     */
    private void update(Chapter chapter){
        chapterMapper.updateByPrimaryKey(chapter);
    }
    /**
     * create by: lin
     * description: 删除
     * create time: 2020/8/2 17:41
     */
    public void delete(String id){
        chapterMapper.deleteByPrimaryKey(id);
    }
}
